public class Group {
	public String GroupName;
	private UserListNode Head;
	private UserListNode Tail;
	public Group Next;

	// Adding group to the head of the groups
	public void addGroupHead(User headOfGroup) {
		Head = new UserListNode();
		Tail = Head;
		Head.CurrUser = headOfGroup;
	}

	// Sending message
	public void sendMessage(String inUser, String message) {
		UserListNode userLists = Head;
		while (userLists != null) {
			userLists.CurrUser.sendMessageToUser(inUser, message, GroupName);
			userLists = userLists.Next;
		}
	}

	public boolean isEmptyGroup() {
		if (Head == null) {
			return true;
		}
		return false;
	}

	// Removing user from group
	public void removeUserFromGroup(String userName) {

		if (Head.CurrUser.Name.equals(userName)) {
			Head = Head.Next;
			return;
		}
		UserListNode userLists = Head;
		boolean found = false;
		while (userLists.Next != null || !found) {
			if (userLists.Next.CurrUser.Name.equals(userName)) {
				userLists.Next.CurrUser
						.removeConversationGroupFromUser(this.GroupName);
				userLists.Next = userLists.Next.Next;
				found = true;
			}
			userLists = userLists.Next;
		}

	}

	// Searching user by it string
	private User searchUser(String userNameSearch) {
		if (Head.CurrUser.Name.toString().equals(userNameSearch)) {
			return Head.CurrUser;
		}
		UserListNode userLists = Head.Next;
		while (userLists != null) {
			if (userLists.CurrUser.Name.toString().equals(userNameSearch)) {
				return userLists.CurrUser;
			}
			userLists = userLists.Next;
		}
		throw new RuntimeException("no such user exists");
	}

	public String toString() {
		String retVal = GroupName + "\n";
		UserListNode temp = Head;
		while (temp != null) {
			retVal += temp.CurrUser.Name + "\n";
			temp = temp.Next;
		}
		return retVal;
	}

	// Adding user to the specific group
	public void addUser(User user) {
		if (Head != null) {
			UserListNode tmp2 = new UserListNode();
			tmp2.CurrUser = user;
			Tail.Next = tmp2;
			Tail = Tail.Next;
		} else {
			Head = new UserListNode();
			Head.CurrUser = user;
			Tail = Head;
		}
	}
}